/**
 * Project Reactor
 */


#ifndef _TCPCONNECTION_H
#define _TCPCONNECTION_H
#include "SocketIO.h"
#include <string>
#include <iostream>
#include "Socket.h"
#include "InetAddress.h"
#include <sstream>
#include <memory>
#include <functional>

using std::ostringstream;
using std::cout;
using std::endl;
using std::string;
using std::shared_ptr;
class TcpConnection ;
using TcpConnectionPtr = shared_ptr<TcpConnection>;
using TcpConnectionCallback = std::function<void(const TcpConnectionPtr&)>;

class TcpConnection 
:public std::enable_shared_from_this<TcpConnection>
{
private:
    SocketIO m_sockIO;
    Socket m_socket;
    InetAddress m_local_addr;
    InetAddress m_peer_addr;
    TcpConnectionCallback m_onNewConnection;
    TcpConnectionCallback m_onMessage;
    TcpConnectionCallback m_onClose;

    InetAddress getLocalAddr();
    InetAddress getPeerAddr();
public: 
/**
 * @param fd
 */
explicit TcpConnection(int fd);
    
~TcpConnection();
    
string receive();
    
/**
 * @param msg
 */
void send(const string & msg);

string streamToString();

Socket & getSock();

bool isClosed();



void setNewConnectionCallback(const TcpConnectionCallback& tcc);
    
/**
 * @param tcc
 */
void setMessageCallback(const TcpConnectionCallback& tcc);
    
/**
 * @param tcc
 */
void setCloseCallback(const TcpConnectionCallback& tcc);
    
void handleNewConnectionCallback();
    
void handleMessageCallback();
    
void handleCloseCallback();
};
#endif //_TCPCONNECTION_H